<?php

namespace PhalconTutorial\Controllers;
use Phalcon\Tag;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use PhalconTutorial\Forms\UsersForm;
use PhalconTutorial\Models\Users;

class UsersController extends ControllerBase
{
	public function initialize()
	{
		//$this->view->setTemplteBefore('private');
		$this->view->setTemplateBefore('skins/frontend/default/template');
	}

	public function indexAction()
	{
		$this->persistent->conditions = null;
		$this->view->form = new UsersForm();
	}

	public function searchAction()
	{
		$numberPage = 1;
		if($this->request->isPost())
		{			
			$query = Criteria::fromInput($this->di, 'PhalconTutorial\Models\Users', $this->request->getPost());
            $this->persistent->searchParams = $query->getParams();
		}
		else
		{
			$numberPage = $this->request->getQuery("page", "int");
		}

		$parameters = array();
		if ($this->persistent->searchParams) {
            $parameters = $this->persistent->searchParams;
        }

        $users = Users::find($parameters);

        
        /*foreach ($users as $user) {
        	die(print_r($user->passwordChanges));
        }*/
        
        if(count($users) == 0)
        {
        	$this->flash->notice("the search dit not find any users");
        	return $this->dispatcher->forward(array(
        		"action" => "index"
        	));
        }

        $paginator = new Paginator(array(
            "data" => $users,
            "limit" => 10,
            "page" => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
	}
}